package com.bob.light.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter
import com.bob.light.R
import com.bob.light.setOnSingleClickListener

class MainPagerAdapter(private var mLayoutInflater: LayoutInflater) : PagerAdapter() {

    private val mTabNames = arrayListOf("家庭管理", "设备配网", "设备管理")
    private var startActivityListener: ((position: Int) -> Unit)? = null

    fun setOnStartActivityListener(block: (position: Int) -> Unit): MainPagerAdapter {
        this.startActivityListener = block
        return this@MainPagerAdapter
    }

    override fun isViewFromObject(view: View, any: Any): Boolean = (view == any)

    override fun getCount(): Int = mTabNames.size

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val rootView = null
        val convertView = mLayoutInflater.inflate(R.layout.app_adapter_main_vp, rootView, false)
        convertView.findViewById<TextView>(R.id.tv_adapter_main)?.let { tv ->
            tv.text = mTabNames[position]
        }
        convertView?.setOnSingleClickListener {
            startActivityListener?.invoke(position)
        }
        container.addView(convertView)
        return convertView
    }

    override fun destroyItem(container: ViewGroup, position: Int, any: Any) {
        if (any is View) {
            container.removeView(any)
        }
    }
}